package cn.cau.control;

import cn.cau.entity.AuditingApplyRecord;
import cn.cau.entity.CodeManageRecord;
import cn.cau.entity.CodeRule;
import cn.cau.entity.CommonUser;
import cn.cau.service.AuditingListService;
import cn.cau.service.CodeManageSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class CodeManageSearchController {
    @Autowired
    private CodeManageSearchService codeManageSearchService;
    @Autowired
    private AuditingListService auditingListService;
    /**
     * 历史申请查询
     *
     * @return
     */
    @RequestMapping(value = "/codeManageList", method = RequestMethod.GET)
    public ModelAndView codeManageList(HttpSession session)  {
        List<CodeManageRecord> codeManageRecords=null;
        List<AuditingApplyRecord> records = new ArrayList<>();   //总待审核表
        if (session.getAttribute("userInfo") != null) {
            //如果用户登录
            CommonUser commonUser = (CommonUser) session.getAttribute("userInfo");
            codeManageRecords = codeManageSearchService.showCodeRuleManageList(commonUser.getUserId());
            for(CodeManageRecord apply : codeManageRecords){
                AuditingApplyRecord temp_record = new AuditingApplyRecord();
                int old_id = apply.getOldCodeRuleId(),new_id = apply.getNewCodeRuleId();
                CodeRule rule = new CodeRule();

                int op = apply.getManageOperation();
                if(op==0){
                    temp_record.setOperation("修改申请");
                }
                else if(op==1){
                    temp_record.setOperation("废除申请");
                }
                else{
                    temp_record.setOperation("设计申请");
                }

                if(op==1){
                    rule = auditingListService.getReluById(old_id);  //如果为废除申请，则对old_rule进行操作
                }
                else{
                    rule = auditingListService.getReluById(new_id);  //否则，对new_rule进行操作
                }

                temp_record.setResult(rule.getCodeState());
                temp_record.setIndex(apply.getCodeRuleManageId());
                temp_record.setReason(apply.getManageContent());
                temp_record.setOp(op);    //0代表修改，1代表设计，2代表废除
                temp_record.setState(rule.getCodeState());
                temp_record.setOldId(apply.getOldCodeRuleId());
                temp_record.setNewId(apply.getNewCodeRuleId());     //newId中存储申请对应的主表id
                temp_record.setDate(apply.getApplyTime());         //日期
                temp_record.setUserId(apply.getManageManId());       //申请人
                temp_record.setObjectName(rule.getCodeRuleName());  //获取名称
                //
                if(op!=2)
                    records.add(temp_record);
            }
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("records", records);
        modelAndView.setViewName("codeManage/codeManageList");
        return modelAndView;
    }
}
